<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\BaseController;
use app\service\AllocationService;
use app\service\UserService;
use think\Request;

class AllocationController extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
      $page = $request->param('page', 1);
      $pageSize = $request->param('pageSize', env('pager.default.size'));
      $baseFrom = $request->param('base_from', []);
      $baseTo = $request->param('base_to', []);
      $status = $request->param('status', []);
      $conditions = [];
      if($baseTo){
        $conditions[] = ['base_to', $baseTo, 'IN'];
      }
      if($baseFrom){
        $conditions[] = ['base_from', $baseFrom, 'IN'];
      }
      if($status){
        $conditions[] = ['status', $status, 'IN'];
      }
      $order = ['allocation_time' => 'desc', 'id' => 'desc'];
      $list = AllocationService::getInstance()->getDataByCondition($conditions, $order);
      // 门店
      $baseList = UserService::getInstance()->storeAll();
      $baseList = array_column($baseList, 'basename', 'base');
      foreach ($list as &$value) {
        $value['base_from_name'] = isset($baseList[$value['base_from']]) ? $baseList[$value['base_from']] : '';
        $value['base_to_name'] = isset($baseList[$value['base_to']]) ? $baseList[$value['base_to']] : '';
      }
      // 分页
      $count = count($list); //总条数
      $start=intval(($page - 1) * $pageSize); //偏移量，当前页-1乘以每页显示条数
      $pager = [
        'totalCount' => $count,
        'pageSize' => intval($pageSize),
        'currPage' => intval($page),
        'totalPage' => intval($count/$pageSize)+1
      ];
      $data = array_slice($list, $start, intval($pageSize));
      return $this->send200ResponseWithPager($data,$pager);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
      $detail = $request->param('data', []);
      $allocation_time = $request->param('allocation_time', '');
      $baseFrom = $request->param('base_from', 0);
      $baseTo = $request->param('base_to', 0);
      if (!$detail) {
        return $this->send400Response('缺少data参数');
      }
      if (!$allocation_time) {
        return $this->send400Response('缺少apply_date参数');
      }
      if (!$baseFrom) {
        return $this->send400Response('缺少base_from参数');
      }
      if (!$baseTo) {
        return $this->send400Response('缺少base_to参数');
      }
      $allocation_time = date('Y-m-d', strtotime($allocation_time));
      $data = [
        'detail' => $detail,
        'allocation_time' => $allocation_time,
        'base_from' => $baseFrom,
        'base_to' => $baseTo
      ];
      $result = AllocationService::getInstance()->createAllocation($data);
      if (!$result['status']) {
        return $this->send400Response($result['msg']);
      }
      return $this->send200Response('ok');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
      // 获取调拨单
      $data = AllocationService::getInstance()->getDataByCondition([['id', $id, '=']]);
      $data = isset($data[0]) ? $data[0] : [];
      if ($data) {
        $conditions[] = ['a.allocation_order_id', $id, '='];
        // 获取调拨详情
        $detail = AllocationService::getInstance()->getAllocationJoinStore($conditions);
        $data['detail'] = $detail;
        $baseList = UserService::getInstance()->storeAll();
        $baseList = array_column($baseList, 'basename', 'base');
        $data['base_from_name'] = isset($baseList[$data['base_from']]) ? $baseList[$data['base_from']] : '';
        $data['base_to_name'] = isset($baseList[$data['base_to']]) ? $baseList[$data['base_to']] : '';
      }
      return $this->send200Response($data);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
      $detail = $request->param('data', []);
      $allocationTime = $request->param('allocation_time', '');
      $base_from = $request->param('base_from', '');
      if (!$detail) {
        return $this->send400Response('更新详情不能为空');
      }
      if (!$allocationTime) {
        return $this->send400Response('调拨时间不能为空');
      }
      if (!$base_from) {
        return $this->send400Response('门店不能为空');
      }
      $data = [
        'detail' => $detail,
        'allocation_time' => $allocationTime,
        'base_from' => $base_from,
      ];
      $result =AllocationService::getInstance()->updateMulti($id, $data);
      if (!$result['status']) {
        return $this->send400Response($result['msg']);
      }
      return $this->send200Response('ok');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
      $result = AllocationService::getInstance()->deleteAllocation($id);
      if ($result['status']) {
        return $this->send200Response('ok');
      }
      return $this->send400Response($result['msg']);
    }

    /**
     * 调拨审批
     * @return \think\Response
     */
    public function check(Request $request){
      $check = $request->param('check', '');
      $allocateId = $request->param('allocate_id', '');
      $comment = $request->param('comment', '');
      if ($check === '') {
        return $this->send400Response('check必填');
      }
      if ($allocateId === '') {
        return $this->send400Response('allocate_id必填');
      }
      $result = AllocationService::getInstance()->checkAllocation($allocateId, $check, $comment);
      if ($result['status']) {
        return $this->send200Response('ok');
      }
      return $this->send400Response($result['msg']);
    }
}
